package com.hibikiluler.javalearning.Config.Security.service;

import com.hibikiluler.javalearning.entity.SysMenus;
import com.hibikiluler.javalearning.entity.SysUser;
import com.hibikiluler.javalearning.mapper.SysUserMapper;
import com.hibikiluler.javalearning.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 实现UserDetailsService 实现自定义登录
 * 重写loadUserByUsername方法
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public UserDetails loadUserByUsername(String value) throws UsernameNotFoundException{
        SysUser user;
        SysUser user_new;
        if (redisUtils.haskey("userInfo" + value)) {
            //缓存中存在用户信息，直接从redis中取
            user = (SysUser) redisUtils.getValue("userInfo_" + value);
            redisUtils.expire("userInfo_" + value,5);
        } else{
            // 首先判断是用什么登录的 1. 网页登录 2. 微信小程序登录
            if(value.length() == 28){  //字符长度为28  即为微信小程序登录
                log.info("微信小程序登录");
                user = userMapper.findByopenId(value);
                if(user == null){
                    log.info("微信小程序新用户登录");
                    String randomUsername= RandomStringUtils.randomAlphanumeric(10); //随机生成字符串
                    userMapper.insertOpenid(randomUsername,value);
                    user_new = userMapper.findByUsername(value);
                    userMapper.insertUserRoles(user_new.getId(), 3L);
                    return user_new;
                }
            }else {
                log.info("网页登录");
                user = userMapper.findByUsername(value);  //如果value是用户名 那就是用户名查找
            }

                if (user == null) {
                    throw new UsernameNotFoundException("用户名或密码错误！");
                }


            if (user.isAdmin()){
                //是管理员 设置为管理员，获取相应信息
                user.setRoles(userMapper.findRoles(null));
                user.setAdminMap(userMapper.findAdmins(null));
                //获取父级菜单
                List<SysMenus> menus = userMapper.findMenus(null);
                //获取子级菜单
                menus.forEach(item -> item.setChildren(userMapper.findChildrenMenus(item.getId(), null)));
                user.setMenuMap(menus);
            } else {
                //非管理员情况下获取的相应信息
                user.setRoles(userMapper.findRoles(user.getId()));
                user.setAdminMap(userMapper.findAdmins(user.getId()));
                //获取父级菜单
                List<SysMenus> menus = userMapper.findMenus(user.getId());
                //获取子级菜单
                menus.forEach(item -> item.setChildren(userMapper.findChildrenMenus(item.getId(), user.getId())));
                user.setMenuMap(menus);
            }
            redisUtils.setValueTime("userInfo_" + value,user,10);
        }
        return user;
    }
}
